package com.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.health.pojo.Order;
import com.health.service.OrderService;
import com.health.utils.Constant;
import com.health.vo.PageResult;
import com.health.vo.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 对预约信息的增删改查
 * 预约列表
 */
@RestController
@RequestMapping("/order")
@PreAuthorize("hasRole('ROLE_ADMIN')") //拥有ROLE_ADMIN角色才可以访问当前Controller下的方法
public class OrderController {

    @Reference
    private OrderService orderService;

    /**
     * 预约列表分页查询
     */
    @GetMapping("/findPage/{pageNow}/{pageSize}")
    public Result findPage(@PathVariable Integer pageNow, @PathVariable Integer pageSize,
                           String query) {

        PageResult<Order> orderList = orderService.findPage(pageNow, pageSize, query);
        return new Result(true, "查询预约列表成功", orderList);
    }

    /**
     * 新增电话预约-查询预约详情
     */
    @GetMapping("/findDetail/{id}")
    public Result findDetail(@PathVariable Integer id) {
        Map<String, Object> detail = orderService.findDetail(id);
        return new Result(true, "查询预约详情成功", detail);
    }

    /**
     * 添加预约
     */
    @PostMapping("/add")
    public Result add(@RequestBody Order order) {
        //预约方式-电话预约
        order.setOrderType(Constant.ORDER_TYPE_PHONE);
        //到诊状态
        order.setOrderStatus(Constant.ORDER_STATUS_NO);
        //添加预约信息，获取预约id
        int orderId = orderService.add(order);
        return new Result(true, "预约成功", orderId);
    }

    /**
     * 判断是否重复预约
     */
    @PostMapping("/checkOrder")
    public Result checkOrder(@RequestBody Order order) {
        int count = orderService.findCountByCondition(order);

        if (count > 0) {
            return new Result(false, "已预约过，不能重复预约");
        }
        return new Result(true, "预约信息正常");
    }

    /**
     * 删除预约
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id){
        int count = orderService.deleteById(id);
        if (count > 0) {
            return new Result(true, "删除成功");

        }
        return new Result(false, "删除失败");
    }

    /**
     * 修改预约信息
     */
    @PostMapping("/update")
    public Result update(@RequestBody Order order){
        int count = orderService.update(order);
        if (count > 0) {
            return new Result(true, "修改预约信息成功");
        }
        return new Result(false,"修改预约信息失败");
    }

    /**
     * 通过id获得预约
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
        Order order = orderService.findById(id);
        return new Result(true,"查询成功",order);
    }
}
